<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: MenuController.php 469 2012-02-13 12:30:31Z beimuaihui@gmail.com $
 */



class System_MenuController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'Menu' );
		$this->menu = new Menu ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		$params=$this->getRequest()->getParams();
		$form_url=$params;

		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		
		$cm['menu_name2']=array("sortable"=>true,"width"=>300);
		$cm['menu_name']=array("hidden"=>true);
		$cm['key_name']=array("sortable"=>true);
		//$cm['menu_pid2']=array("sortable"=>true);
		$cm['menu_pid']=array("hidden"=>true);
		$cm['is_show2']=array("sortable"=>true);
		$cm['is_show']=array("hidden"=>true);
		$cm['state2']=array("sortable"=>true);
		$cm['state']=array("hidden"=>true);
		$cm['ordering']=array("sortable"=>true);
		//$cm['is_page2']=array("sortable"=>true);
		//$cm['is_page']=array("hidden"=>true);
		$cm['url']=array("sortable"=>true);
		//$cm['is_permission']=array("sortable"=>true);
		$cm['img_icon2']=array("sortable"=>true);
		$cm['img_icon']=array("hidden"=>true);
		$cm['description']=array("hidden"=>true);
		$cm['model_id']=array("hidden"=>true);
		$cm['menu_id']=array("cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;		
		
		$form_url['action']="data";
		$grid=new Baogg_View_Extgrid($form_url, $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
		$grid->addExistButtons(array('add','remove','query'),array('edit','delete'));
		Baogg_View_Extgrid::$PAGE_SIZE=3000;
		
		$this->view->form_url=BAOGG_BASE_URL."system/menu/";
		
		$rs_parents=$this->menu->getList(array("state"=>1,"is_permission"=>0),NULL,NULL,array("menu_id","menu_name","menu_pid"));			
		
		$objTree=new Baogg_Db_Tree();
		$tr_rs_parents=$objTree->rs2ComboTree($rs_parents);		
		$store_menu_pid=$objTree->tr2ComboStore($tr_rs_parents);
		//echo '<pre>'.__FILE__.__LINE__;var_export($rs_parents);exit;
		//$form_url=BAOGG_BASE_URL."system/menu/";		
		/*$tabs=array("items"=>array(
									array("title"=>Baogg_Language::get("details"),"column_num"=>3),
									array("title"=>Baogg_Language::get("more"),"column_num"=>2),
								)
					);
					$form=,$tabs);
					*/	
		$form_url['action']='add';
		$form=new Baogg_View_Extform(array("url"=>$form_url,"height"=>500,"width"=>600));
		
		$form->addItem(0,array('name'=>'menu_name'));
		$form->addItem(0,array('name'=>'menu_pid','xtype'=>'combo','store'=>$store_menu_pid));
		$form->addItem(0,array('name'=>'key_name'));
		$form->addItem(1,array('name'=>'is_show','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(1,array('name'=>'state','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(1,array('name'=>'ordering'));
		//$form->addItem(2,array('name'=>'is_page','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::array2store("arr_state")));
		$form->addItem(2,array('name'=>'url'));
		
		$model=new Model();
		$rs_model=$model->getList(array("state"=>1,"menu_id"=>(int)$params['menu_id']),array("sort"=>"ordering","dir"=>"asc"),NULL,array("model_id","model_name"));
		$arr_model=array(array('0',BAOGG_LANGUAGE::get('please_select')));
		foreach((array)$rs_model as $v){
			$arr_model[]=array($v['model_id'],$v['model_name']);
		}
		$form->addItem(2,array('name'=>'model_id','xtype'=>'combo','store'=>$arr_model));
		
		//$form->addItem(2,array('name'=>'is_permission','xtype'=>'radiogroup','source'=>BAOGG_LANGUAGE::get("arr_state")));
		$arr_icon=Baogg_File::ReadDirFile(BAOGG_UPLOAD_DIR.'image/menu/');
		$store_icon=array();
		foreach((array)$arr_icon as $v){
			$store_icon[]=array($v,$v);
		}
		//echo '<pre>';print_r($store_icon);exit;
		$form->addItem(2,array('name'=>'img_icon','xtype'=>'combo','store'=>$store_icon,'tpl'=>'<tpl for="."><div class="x-combo-list-item" ext:qtip="&lt;img src=&#39;'.BAOGG_UPLOAD_URL.'image/menu/{text:htmlEncode}&#39; /&gt;"><img src="'.BAOGG_UPLOAD_URL.'image/menu/{text:htmlEncode}"  /> {text:htmlEncode}</div></tpl>'));
		$form->addItem(3,array('name'=>'description','xtype'=>'htmleditor','height'=>75));
		$form->addItem(3,array('name'=>'menu_id',"xtype"=>"hidden","hideLabel"=>true));		
		$form->addExistButtons(array('-','save_grid','save_new','save','-','search','-','cancel'));//,'config','config'
		$this->view->form=$form;
		
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		
		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction()
	{		
		$this->menu->updatePids();
		
	    $post=$this->_request->getPost ();
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="menu_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else if($k=="query"){
	           		$where[]="(menu_name like '%{$v}%' or key_name like '%{$v}%' or url like '%{$v}%')";
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	   $order=$order?$order:array("sort"=>"ordering","dir"=>"asc");
	   
	   $rs_cnt=$this->menu->getList($where,$order,NULL,"count(*) as cnt");
	   
	   $rs_tr=$this->menu->getList($where,NULL,NULL,array("menu_pids"));
	   $pids="0,";
	   foreach($rs_tr as $v){
	   	 $pids.=$v['menu_pids'];
	   }
	   $arr_pids=array_filter(array_unique(explode(",",$pids)));
	   $where=array("menu_id"=>$arr_pids);
		$rs=$this->menu->getList($where,$order,NULL,array("menu_id","menu_name as menu_name2","menu_pid","*"));
		
		
		$arr_state=BAOGG_LANGUAGE::get("arr_state");
		foreach((array)$rs as $k=>$v){
			$rs[$k]['is_show2']=$arr_state[$v['is_show']];
			$rs[$k]['state2']=$arr_state[$v['state']];
			//$rs[$k]['is_page2']=$arr_state[$v['is_page']];
			$rs[$k]['img_icon2']=$v['img_icon']?'<img src="'.BAOGG_UPLOAD_URL.'image/menu/'.$v['img_icon'].'" />':'';
		}

		
		$objTree=new Baogg_Db_Tree();
		$tr=$objTree->rs2GridTree($rs);		
		$data=$objTree->tr2GridStore($tr);
		
		foreach((array)$data as $k=>$v){
			$this->menu->_update(array('ordering'=>($k+1)*10),$v['menu_id']);
		}
		$this->view->grid = Zend_Json::encode(array("count"=>$rs_cnt[0]['cnt'],"data"=>$data));
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		$this->saveAction();
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		$this->saveAction();
	}

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->menu->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		$this->menu->updatePids();
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();	
	}
	
	function removeAction()
	{
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();
		

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}

		//echo '<Pre>';print_r($this->getRequest()->getParams());print_r($formData);exit;
		
		$arr=$this->menu->filterForm($formData);        
		$rs = $this->menu->getSame($arr);
		$action=$this->getRequest()->getParam('action');
		
		if( $action=='edit'){		
		    $this->menu->_update($arr,$arr["menu_id"]);
		    $ret['success'] = true;				
			$ret['msg'] = "save_ok";		
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
				$ret['success'] = $this->menu->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
		$this->menu->updatePids();
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
	
	}

}
